﻿Namespace EIBData.EIS
    Public NotInheritable Class EIBDataEIS_3
        Inherits EIBDataEIS_Base

        Protected _day As Enums.EIBData_Days = Nothing
        Protected _hour As Byte = Nothing
        Protected _minute As Byte = Nothing
        Protected _second As Byte = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data
            _day = (data(1) And &HE0) >> 5
            _hour = data(1) And &H1F
            _minute = data(2) And &H3F
            _second = data(3) And &H3F

            If _hour > 23 Then Throw New EIB.Exceptions.EIBDataDecodeException("")
            If _minute > 59 Then Throw New EIB.Exceptions.EIBDataDecodeException("")
            If _second > 59 Then Throw New EIB.Exceptions.EIBDataDecodeException("")
        End Sub

        Sub New(hour As Byte, minute As Byte, second As Byte)
            Me.New(Enums.EIBData_Days.NoDay, hour, minute, second)
        End Sub

        Sub New(day As Enums.EIBData_Days, hour As Byte, minute As Byte, second As Byte)
            If Not [Enum].IsDefined(GetType(Enums.EIBData_Date.EIBData_Days), day) Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If hour > 23 Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If minute > 59 Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If second > 59 Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _day = day
            _hour = hour
            _minute = minute
            _second = second
            MyBase._data = New Byte() {0, ((_day And &H7) << 5) + (_hour And &H1F), (_minute And &H3F), (_second And &H3F)}
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = EIS_OctetLength
        End Function

        Public Overrides Function ToString() As String
            If _day > 0 AndAlso _day < 8 Then
                Return String.Format("{0}, {1}:{2}:{3}", _day, _hour, _minute, _second)
            Else
                Return String.Format("{0}:{1}:{2}", _hour, _minute, _second)
            End If
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_day, _hour, _minute, _second}
        End Function

        Public Overrides Function ToInfoList() As List(Of EIB.InfoBase)
            Dim list As New List(Of EIB.InfoBase)

            list.Add(New EIB.Info("Type", EIS_Name))
            list.Add(New EIB.Info("Day", String.Format("{0} (0x{0:X})", _day)))
            list.Add(New EIB.Info("Hour", _hour))
            list.Add(New EIB.Info("Minute", _minute))
            list.Add(New EIB.Info("Second", _second))

            Return list
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(Enums.EIBData_Days), GetType(Byte), GetType(Byte), GetType(Byte)}
            End Get
        End Property

        Public Shared Shadows ReadOnly Property EIS_OctetLength As Byte
            Get
                Return 4
            End Get
        End Property

        Public Shared Shadows ReadOnly Property EIS_Name As String
            Get
                Return "EIS 3"
            End Get
        End Property


        Public Shared Function FromDateTime(value As DateTime) As EIBDataEIS_3
            Return New EIBDataEIS_3(Enums.EIBData_Date.DayOfWeekToDay(value.DayOfWeek), value.Hour, value.Minute, value.Second)
        End Function

        Public ReadOnly Property ToDateTime As DateTime
            Get
                Return New DateTime(1, 1, _day, _hour, _minute, _second)
            End Get
        End Property

        Public ReadOnly Property Day As Enums.EIBData_Days
            Get
                Return _day
            End Get
        End Property

        Public ReadOnly Property DayOfWeek As DayOfWeek
            Get
                Return Enums.EIBData_Date.DayToDayOfWeek(_day)
            End Get
        End Property

        Public ReadOnly Property Hour As Byte
            Get
                Return _hour
            End Get
        End Property

        Public ReadOnly Property Minute As Byte
            Get
                Return _minute
            End Get
        End Property

        Public ReadOnly Property Second As Byte
            Get
                Return _second
            End Get
        End Property
    End Class
End Namespace